﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using System.Data;


namespace HRM
{
    public partial class frmGradeDetailInfo : Form
    {
        int _GradeDetailID;
        GradeDetailInfo _GDInfo;
        DialogResult dlgResult;
        GradeDetailClient wcfGD;
        RoleRightInfo listRole;
        GradeClient _wcfGrade;
        LevelClient _wcfLevel;

        public frmGradeDetailInfo(int GradeDetailID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //LoadLanguage();
            _GradeDetailID = GradeDetailID;
            wcfGD = new GradeDetailClient(ProxyConfig.GetBinding(), ProxyConfig.GradeDetail);
            _wcfGrade = new GradeClient(ProxyConfig.GetBinding(), ProxyConfig.Grade);
            _wcfLevel = new LevelClient(ProxyConfig.GetBinding(), ProxyConfig.Level);
            dlgResult = DialogResult.Cancel;
            _GDInfo = new GradeDetailInfo();
            LoadBranch();
            LoadLevel();
            if (_GradeDetailID > 0)
            {
                _GDInfo = wcfGD.GradeDetail_GetInfo(_GradeDetailID);
                Info2Form();
            }
            TinhMucLuong();
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        private void LoadBranch()
        {
            BranchClient wcf = Service.Branch();
            DataTable dt;
            if (!Program.isAdmin && !Program.isViewAll)
            {
                dt = wcf.Branch_GetOne(Program.intBranchID).Tables[0];
            }
            else
            {
                dt = wcf.Branch_GetAll().Tables[0];
            }

            cbBranchCode.DisplayMember = "Branch_Name";
            cbBranchCode.ValueMember = "Branch_Code";
            cbBranchCode.DataSource = dt;
        }
        void LoadGrade(string _BranchCode)
        {
            MainServiceClient _wcfMain = Service.MainService();
            DataTable dtGrade;
            string _sql = "";
            _sql = "SELECT Grade_ID,Grade_Name,Grade_BranchCode FROM HRM_Grade WHERE Grade_BranchCode= '" + _BranchCode + "' ORDER BY Grade_Name";
            dtGrade = _wcfMain.Query(_sql).Tables[0];
            cboGrade.DisplayMember = "Grade_Name";
            cboGrade.ValueMember = "Grade_ID";
            cboGrade.DataSource = dtGrade;
        }

        void LoadLevel()
        {
            cboLevel.DisplayMember = "Level_Name";
            cboLevel.ValueMember = "Level_ID";
            cboLevel.DataSource = _wcfLevel.Level_GetAll().Tables[0];
        }

        private void Info2Form()
        {
            cbBranchCode.SelectedValue = _GDInfo.BranchCode;
            cboGrade.SelectedValue = _GDInfo.GradeID;
            cboLevel.SelectedValue = _GDInfo.LevelID;
            txtAmount.Text = _GDInfo.Amount.ToString(UIParamater.Cham2So);
            txtMonth.Text = _GDInfo.MonthValue.ToString();
        }

        void Form2Info()//Chuyển dữ liệu vừa nhập vào info
        {
            _GDInfo.ID = _GradeDetailID;
            _GDInfo.BranchCode = Program.strBranchBHXH;
            _GDInfo.GradeID = Convert.ToInt32(cboGrade.SelectedValue);
            _GDInfo.LevelID = Convert.ToInt32(cboLevel.SelectedValue);
            _GDInfo.Amount = Convert.ToDecimal(txtAmount.Text);
            _GDInfo.MonthValue = Convert.ToInt32(txtMonth.Text);
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            decimal _dec;
            if (!decimal.TryParse(txtAmount.Text, out _dec))
            {
                txtAmount.Focus();
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), "Hệ số lương không hợp lệ", UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                return;
            }
            if (_GDInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _GDInfo = new GradeDetailInfo();
            _GradeDetailID = 0;
            txtAmount.Text = "0";

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                TinhMucLuong();
                Form2Info();
                if (_GradeDetailID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    if (wcfGD.GradeDetail_Update(_GDInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        dlgResult = DialogResult.OK;
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    _GradeDetailID = _GDInfo.ID = wcfGD.GradeDetail_Insert(_GDInfo);
                    if (_GradeDetailID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        dlgResult = DialogResult.OK;
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void frmGradeDetailInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmGradeDetailInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void txtAmount_Leave(object sender, EventArgs e)
        {
            txtAmount.Text = UISetting.GetDecimalNo(txtAmount.Text).ToString(UIParamater.Cham2So);
            TinhMucLuong();
        }
        private void TinhMucLuong()
        {
            decimal hesoluong = 0, luongtoithieu = 0, mucluong = 0;
            hesoluong = UISetting.GetDecimalNo(txtAmount.Text);
            ParameterClient wcfParameter = Service.Parameter();
            luongtoithieu = UISetting.GetDecimalNo(wcfParameter.Parameter_GetByName("MinBasicSalary").Value);
            mucluong = hesoluong * luongtoithieu;
            textBox1.Text = mucluong.ToString("#,##0");
        }

        private void cbBranchCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string _BranchCode = "";
            _BranchCode = cbBranchCode.SelectedValue.ToString();
            LoadGrade(_BranchCode);
        }

    }
}
